/******************************************************************************

  File Name     : face_demo.c
  Version       : Initial Draft
  Author        : tongzhilin
  Created       : 2020/8/6
  Last Modified :
  Description   : face_demo_roc, 3000*3000 images(112*112), use feature model, run
  Function List :
  History       :
  1.Date        : 2020/9/3
    Author      : tongzhilin
    Modification: Created file

******************************************************************************/
#include "image_container.h"
#include "mp_common.h"
#include "alg_struct.h"
#include "alg_api.h"

INT32 main()
{
    INT32 iSuccess = 0;
    char cLineBuff[256] = { 0 };
    FILE* pfConfig = NULL;
    FILE* pfResult = NULL;
    INT32 s32C = 0;
    INT32 s32H = 0;
    INT32 s32W = 0;
    UINT64 u64TimeStart = 0;
    ALG_INIT_PARAMS stInitParams;
    ALG_FACELMK_RESULT_S stFaceLmkRst;

#ifdef ARCH_AARCH64
    //arm64 & arm
    system("echo 3 > /proc/sys/vm/drop_caches");
#endif

    pfConfig = fopen("/home/tzl/image_db/eval_face_feature/test3000_1.txt", "r+");
    if(!pfConfig)
    {
        MP_LOG_ERR("Open image list error!\n");
        return 0;
    }
    pfResult = fopen("./feature_result_for_roc.txt", "w+");
    if(!pfResult)
    {
        MP_LOG_ERR("Open feature result file error!\n");
        return 0;
    }

    char cFaceLmkModel[] = "../../models/nnie/pfpld_merge_bn.wk";

    memset(&stInitParams, 0, sizeof(ALG_INIT_PARAMS));
    strcpy(stInitParams.unModel.stNnie.m_cWkFile, cFaceLmkModel);
    ALG_HANDLE pstFaceLmkV1 = Alg_Face_LMK_Init(&stInitParams);

    TIME_START(u64TimeStart);
    while(1)
    {
        memset(cLineBuff, 0, sizeof(cLineBuff));
        if(fgets(cLineBuff, sizeof(cLineBuff), pfConfig) == NULL)
        {
            break;
        }

        if (cLineBuff[strlen(cLineBuff) - 1] == '\r' || cLineBuff[strlen(cLineBuff) - 1] == '\n')
        {
            cLineBuff[strlen(cLineBuff) - 1] = '\0';
        }

        s32C = Alg_Face_Get_Input_C(pstFaceLmkV1);
        s32H = Alg_Face_Get_Input_H(pstFaceLmkV1);
        s32W = Alg_Face_Get_Input_W(pstFaceLmkV1);
        MP_LOG_INFO("Face landmark input image c : %d, h : %d, w : %d , pic:%s, num:%d\n", s32C, s32H, s32W, cLineBuff, iSuccess);
        cv::Mat image = cv::imread(cLineBuff, cv::IMREAD_COLOR);
        if (image.empty())
        {
            MP_LOG_ERR("imread error, file name:%s\n", cLineBuff);
            return 0;
        }
        //cv::Mat image_rgb;
        //cv::cvtColor(image, image_rgb, CV_BGR2RGB);
        MP_ASSERT(s32W == image.cols && s32H == image.rows);
        ALG_IMAGE_S stAlgImage = cv2alg(image, 16);
        memset(&stFaceLmkRst, 0, sizeof(stFaceLmkRst));
        Alg_Face_Forward_Image(pstFaceLmkV1, &stAlgImage, 1, &stFaceLmkRst);

        for(INT32 i = 0; i < stFaceLmkRst.m_s32PointsNum; i++)
        {
            cv::circle(image,
                cv::Point{(int)stFaceLmkRst.m_stPoints[i].m_f32X, (int)stFaceLmkRst.m_stPoints[i].m_f32Y},
                1, cv::Scalar{255, 255, 0}, -1, 1);
        }
        std::string savepic1 = "../../images/_save_pic/";
        savepic1 += std::to_string(iSuccess);
        savepic1 += ".bmp";
        cv::imwrite(savepic1, image);

        iSuccess++;
    }

    TIME_END(u64TimeStart, "Get lmk success:%d,time:", iSuccess);

    INT32 s32FdResult = fileno(pfResult);
    fsync(s32FdResult);
    fclose(pfConfig);
    fclose(pfResult);

    return 0;
}
